//
//  CWCReadingViewController.m
//  ChuanNews
//
//  Created by dllo on 16/4/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CWCReadingViewController.h"
#import "BaseHeader.h"
#import "CWCDataModel.h"
#import "CWCCostomCell.h"
#import "CWCDetailReadingViewController.h"
#import "UIImageView+WebCache.h"
#import "CWCSearchViewController.h"
#import "CWCWeatherViewController.h"
@interface CWCReadingViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *readingArray;
@property(nonatomic, assign)BOOL isUpLoading;
@property(nonatomic, retain)MBProgressHUD *hud;
@property(nonatomic, assign)NSInteger page;
@end

@implementation CWCReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"阅读";
    self.readingArray = [NSMutableArray array];
    self.isUpLoading = NO;
    self.hud = [[MBProgressHUD alloc]init];
    self.hud.labelText = @"loading......";
    self.hud.detailsLabelText = @"网络正在加载,请稍后......";
    self.hud.color = [UIColor blackColor];
    self.hud.dimBackground = YES;
    self.page = 20;
    [self creatDataWith:self.page];
    [self creatView];
    self.isUpLoading = NO;
    [self addFooter];
    [self addHeader];

  
}
- (void)addHeader{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        [self creatDataWith:self.page];
    }];
}
- (void)addFooter {
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.isUpLoading = YES;
        self.page += 10;
        [self creatDataWith:self.page];
    }];
}

-(void)creatDataWith:(NSInteger)page{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://c.3g.163.com/recommend/getSubDocPic?from=yuedu&size=%ld",self.page] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (self.isUpLoading == NO) {
            [self.readingArray removeAllObjects];
        }
        NSDictionary *dic = responseObject;
        NSMutableArray *readArr = dic[@"推荐"];
        for (NSDictionary *readingDic in readArr) {
            CWCDataModel *readModel = [[CWCDataModel alloc]init];
            [readModel setValuesForKeysWithDictionary:readingDic];
            [self.readingArray addObject:readModel];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.hud hide:YES afterDelay:0];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
        });
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络获取数据失败");
        
    }];
}
-(void)creatView{
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView. delegate = self;
    [self.tableView release];
    [self.tableView registerClass:[CWCCostomCell class] forCellReuseIdentifier:@"CWCCostomCell"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 25, 25);
    [button addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = buttonItem;
    
    UIButton *weatherButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [weatherButton setImage:[UIImage imageNamed:@"tianqi.png"] forState:UIControlStateNormal];
    weatherButton.frame = CGRectMake(0, 0, 25, 25);
    [weatherButton addTarget:self action:@selector(weatherButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *weatherItem = [[UIBarButtonItem alloc]initWithCustomView:weatherButton];
    self.navigationItem.rightBarButtonItem = weatherItem;
    
    
}
-(void)weatherButtonClick:(UIButton *)button{
    CWCWeatherViewController *vc = [[CWCWeatherViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


-(void)search:(UIButton *)button{
    CWCSearchViewController *vc = [[CWCSearchViewController alloc]init];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.readingArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CWCDataModel *reading = self.readingArray[indexPath.row];
    
    CWCCostomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CWCCostomCell"];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:reading.imgsrc]];
    cell.titleLabel.text = reading.title;
    cell.digestLabel.text = reading.source;
    return cell;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 120;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CWCDetailReadingViewController * vc = [[CWCDetailReadingViewController alloc]init];
    CWCDataModel *reading = [self.readingArray objectAtIndex:indexPath.row];
    vc.reading = reading;
    vc.id = reading.docid;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    [_tableView release];
    [_hud release];
    [super dealloc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
